<?php


namespace app\partner\controller;


class Earnings extends Common
{
    /**
     * 账户余额
     */
    public function accountBalance()
    {
        $id = $this -> id;
        $uid = $this -> uid;
        //上上月第一天和最后一天时间戳
        $last_mon_last_start = strtotime(date('Y-m-01', strtotime('-2 month')));
        $last_mon_last_end = strtotime(date('Y-m-t 23:59:59', strtotime('-2 month')));
        //上月第一天和最后一天时间戳
        $last_mon_start = strtotime(date('Y-m-01', strtotime('-1 month')));
        $last_mon_end = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));
        //本月第一天和最后一天时间戳
        $beginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $this_mon_start = strtotime($beginDate);
        $this_mon_end = strtotime(date('Y-m-d 23:59:59', strtotime("$beginDate +1 month -1 day")));
        $time = time();
        $data['yue'] = db('super_partner')
            ->where('id', $id)
            ->value('money');
        $data['tixianzhong'] = db('super_partner')
            ->where('id', $id)
            ->value('freeze_money');
        $data['yitixian'] = db('super_partner_liushui')
            ->where('partner_id', $id)
            ->where('lb', 4)
            ->sum('money');
        $data['lejishouru'] = db('super_partner_liushui')
            ->where('partner_id', $id)
            ->where('money', '>', 0)
            ->sum('money');
        //待结算收益,本月25号结算上月已确认收货的有效订单收益
        //本月25号时间戳
        $jiesuanri = mktime(23,59,59,date('m'), 25,date('Y'));
        if($time < $jiesuanri) {
            $tb_daijiesuan = db('tb_order')
                ->alias('o')
                ->join('user_manage u', 'o.uid=u.id', 'LEFT')
                ->where('u.super_partner_id', $id)
                ->where('order_status', 3)
                ->where('order_time', 'between', [$last_mon_start, $last_mon_end])
                ->sum('super_partner_rebate_fee');
            $jd_daijiesuan = db('jd_order')
                ->alias('o')
                ->join('user_manage u', 'o.uid=u.id', 'LEFT')
                ->where('u.super_partner_id', $id)
                ->where('order_status', 3)
                ->where('order_time', 'between', [$last_mon_start * 1000, $last_mon_end * 1000])
                ->sum('super_partner_rebate_fee');
            $pdd_daijiesuan = db('pdd_order')
                ->alias('o')
                ->join('user_manage u', 'o.uid=u.id', 'LEFT')
                ->where('u.super_partner_id', $id)
                ->where('order_status', 3)
                ->where('order_time', 'between', [$last_mon_start, $last_mon_end])
                ->sum('super_partner_rebate_fee');
            $data['daijiesuan'] = $tb_daijiesuan + $jd_daijiesuan + $pdd_daijiesuan;
        } else {
            $data['daijiesuan'] = 0;
        }
        //本月预估
        $tb_this_yugu = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$this_mon_start, $this_mon_end])
            ->sum('super_partner_rebate_fee');
        $jd_this_yugu = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$this_mon_start * 1000, $this_mon_end * 1000])
            ->sum('super_partner_rebate_fee');
        $pdd_this_yugu = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$this_mon_start, $this_mon_end])
            ->sum('super_partner_rebate_fee');
        $data['this_yugu'] = $tb_this_yugu + $jd_this_yugu + $pdd_this_yugu;
        //上月预估
        $tb_last_yugu = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$last_mon_start, $last_mon_end])
            ->sum('super_partner_rebate_fee');
        $jd_last_yugu = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$last_mon_start * 1000, $last_mon_end * 1000])
            ->sum('super_partner_rebate_fee');
        $pdd_last_yugu = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 'in', [1,2,3])
            ->where('order_time', 'between', [$last_mon_start, $last_mon_end])
            ->sum('super_partner_rebate_fee');
        $data['last_yugu'] = $tb_last_yugu + $jd_last_yugu + $pdd_last_yugu;
        //上月结算,上上月已确认收货的有效订单收益
        $tb_last_jiesuan = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 3)
            ->where('order_time', 'between', [$last_mon_last_start, $last_mon_last_end])
            ->sum('super_partner_rebate_fee');
        $jd_last_jiesuan = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 3)
            ->where('order_time', 'between', [$last_mon_last_start * 1000, $last_mon_last_end * 1000])
            ->sum('super_partner_rebate_fee');
        $pdd_last_jiesuan = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->where('order_status', 3)
            ->where('order_time', 'between', [$last_mon_last_start, $last_mon_last_end])
            ->sum('super_partner_rebate_fee');
        $data['last_jieusan'] = $tb_last_jiesuan + $jd_last_jiesuan + $pdd_last_jiesuan;
        //收支明细
        $where['partner_id'] = $id;
        $input = input();
        $lb = isset($input['lb']) ? $input['lb'] : '';
        if(!empty($lb)) {
            $where['lb'] = $lb;
        }
        $shijian = isset($input['shijian']) ? $input['shijian'] : '';
        if(!empty($shijian)) {
            $start_time = strtotime(date('Y-m-d', strtotime('-'.$shijian.' month')));
            $where['time'] = ['EGT', $start_time];
        }
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;
        $shouzhimingxi = db('super_partner_liushui')
            ->where($where)
            ->order('id desc')
            ->limit($offset, $limit)
            ->field('time,lb,money,yue')
            ->select();
        foreach ($shouzhimingxi as $key => $value) {
            $shouzhimingxi[$key]['time'] = date('Y-m-d H:i:s', $value['time']);
            switch ($value['lb']) {
                case 1:
                    $lb = '淘宝收益结算';
                break;
                case 2:
                    $lb = '京东收益结算';
                break;
                case 3:
                    $lb = '拼多多收益结算';
                break;
                case 4:
                    $lb = '申请提现';
                break;
                case -1:
                    $lb = '淘宝退货扣除';
                break;
                case -2:
                    $lb = '京东退货扣除';
                break;
                case -3:
                    $lb = '拼多多退货扣除';
                break;
                case -4:
                    $lb = '提现失败';
                break;
            }
            $shouzhimingxi[$key]['lb'] = $lb;
            if(in_array($value['lb'], [1,2,3,-4])) {
                $shouzhimingxi[$key]['income'] = '+' . $value['money'];
                $shouzhimingxi[$key]['expend'] = '-';
            } else if(in_array($value['lb'], [-1,-2,-3,4])) {
                $shouzhimingxi[$key]['income'] = '-';
                $shouzhimingxi[$key]['expend'] = $value['money'];
            }
            unset($shouzhimingxi[$key]['money']);
        }
        $total_num = db('super_partner_liushui')
            ->where($where)
            ->count();
        $page_num = ceil($total_num / $limit);
        $data['mingxi'] = $shouzhimingxi;
        $data['total_num'] = $total_num;
        $data['page_num'] = $page_num;
        returnSuccess($data);
    }

    /**
     * 结算记录
     */
    public function settleDetail()
    {
        $id = $this -> id;
//        $id = 1;
        $input = input();
        //收益结算记录
        $nian = isset($input['nian']) ? $input['nian'] : '';
        if(empty($nian)) {
            $nian = date('Y', time());
        }
        $detail = db('super_partner_liushui')
            ->field("order_type,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =1 THEN money ELSE 0 END) yi,   
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =2 THEN money ELSE 0 END) er,
SUM(CASE when FROM_UNIXTIME( time, '%m' ) =3 THEN money ELSE 0 END) san,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =4 THEN money ELSE 0 END) si,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =5 THEN money ELSE 0 END) wu,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =6 THEN money ELSE 0 END) liu,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =7 THEN money ELSE 0 END) qi,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =8 THEN money ELSE 0 END) ba,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =9 THEN money ELSE 0 END) jiu,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =10 THEN money ELSE 0 END) shi,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =11 THEN money ELSE 0 END) sy,
SUM(CASE WHEN FROM_UNIXTIME( time, '%m' ) =12 THEN money ELSE 0 END) se")
            ->where("FROM_UNIXTIME(time,'%Y')=$nian")
            ->where('lb', 'in', [1,2,3])
            ->where('partner_id',$id)
            ->group('order_type')
            ->select();
        $data = [];
        foreach ($detail as $key => $value) {
            switch ($value['order_type']) {
                case 1:
                    $order_type = 'tb';
                break;
                case 2:
                    $order_type = 'jd';
                break;
                case 3:
                    $order_type = 'pdd';
                break;
            }
            $data[$order_type][] = $value['yi'];
            $data[$order_type][] = $value['er'];
            $data[$order_type][] = $value['san'];
            $data[$order_type][] = $value['si'];
            $data[$order_type][] = $value['wu'];
            $data[$order_type][] = $value['liu'];
            $data[$order_type][] = $value['qi'];
            $data[$order_type][] = $value['ba'];
            $data[$order_type][] = $value['jiu'];
            $data[$order_type][] = $value['shi'];
            $data[$order_type][] = $value['sy'];
            $data[$order_type][] = $value['se'];
        }
        //详细记录
        $limit = 10;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;
        $tb_detail = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("DATE_FORMAT(order_time,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->order('shijian desc')
//            ->limit($offset, $limit)
            ->group('shijian')
            ->select();
        $tb_num = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("DATE_FORMAT(order_time,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->group('shijian')
            ->count();
        $jd_detail = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("FROM_UNIXTIME(order_time*0.001,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->order('shijian desc')
//            ->limit($offset, $limit)
            ->group('shijian')
            ->select();
        $jd_num = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("FROM_UNIXTIME(order_time*0.001,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->group('shijian')
            ->count();
        $pdd_detail = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("FROM_UNIXTIME(order_time,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->order('shijian desc')
//            ->limit($offset, $limit)
            ->group('shijian')
            ->select();
        $pdd_num = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->where('u.super_partner_id', $id)
            ->field("FROM_UNIXTIME(order_time,'%Y-%m') as shijian,sum(user_buy_price) as total_money,sum(super_partner_rebate_fee) as total_earn")
            ->group('shijian')
            ->count();
        //上个月时间戳
        $log_detail = [];
        $last_mon = strtotime(date('Y-m-1', strtotime('last month')));
        $this_mon = strtotime(date('Y-m-1', time()));
        for ($i = 0; $i >= 0; $i ++) {
            $line = date('Y-m', $last_mon - 86400 * $i);
            $this_lin = date('Y-m', $this_mon - 86400 * $i);
            if(isset($tb_detail[$i]) == false && isset($jd_detail[$i]) == false && isset($pdd_detail[$i]) == false) {
                break;
            } else {
                $log_detail[$i]['jiesuanshijian'] = date('Y-m-25', strtotime($this_lin));
                $log_detail[$i]['jiesuanzhouqi'] = date('Y年m月', strtotime($line));
                if(isset($tb_detail[$i]['shijian']) && $tb_detail[$i]['shijian'] == $line) {
                    $log_detail[$i]['tb_total_money'] = isset($tb_detail[$i]['total_money']) ? $tb_detail[$i]['total_money'] : 0;
                    $log_detail[$i]['tb_total_earn'] = isset($tb_detail[$i]['total_earn']) ? $tb_detail[$i]['total_earn'] : 0;
                } else {
                    $log_detail[$i]['tb_total_money'] = 0;
                    $log_detail[$i]['tb_total_earn'] = 0;
                }
                if(isset($jd_detail[$i]['shijian']) && $jd_detail[$i]['shijian'] == $line) {
                    $log_detail[$i]['jd_total_money'] = isset($jd_detail[$i]['total_money']) ? $jd_detail[$i]['total_money'] : 0;
                    $log_detail[$i]['jd_total_earn'] = isset($jd_detail[$i]['total_earn']) ? $jd_detail[$i]['total_earn'] : 0;
                } else {
                    $log_detail[$i]['jd_total_money'] = 0;
                    $log_detail[$i]['jd_total_earn'] = 0;
                }
                if(isset($pdd_detail[$i]['shijian']) && $pdd_detail[$i]['shijian'] == $line) {
                    $log_detail[$i]['pdd_total_money'] = isset($pdd_detail[$i]['total_money']) ? $pdd_detail[$i]['total_money'] : 0;
                    $log_detail[$i]['pdd_total_earn'] = isset($pdd_detail[$i]['total_earn']) ? $pdd_detail[$i]['total_earn'] : 0;
                } else {
                    $log_detail[$i]['pdd_total_money'] = 0;
                    $log_detail[$i]['pdd_total_earn'] = 0;
                }
            }
            $last_mon = strtotime(date('Y-m', strtotime($line)));
            $this_mon = strtotime(date('Y-m', strtotime($this_lin)));
        }
        //详细记录数据总条数和页数
        if($tb_num > $jd_num) {
            $total_num = $tb_num;
        } else {
            $total_num = $jd_num;
        }
        if($pdd_num > $total_num) {
            $total_num = $pdd_num;
        }
        $page_num = ceil($total_num / $limit);
        returnSuccess(['earning_detail'=>$data,'log_detail'=>$log_detail,'total_num'=>$total_num,'page_num'=>$page_num]);
    }
}